﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace UFODefense
{
    class MissilePad : DrawableGameComponent
    {
        float m_posX;
        SpriteBatch m_spriteBatch;
        ContentManager m_contentManager;
        SoundEffect m_launchS;
        float m_lastLaunch;
        Texture2D m_launcherT;
        //only one missile can exist at any given time
        Missile m_missile;

        Vector2 m_target;
        double m_lastShotTime;
        SoundEffect m_laserS;
        Texture2D m_crosshairT;
        Laser m_laser;
        Random m_randomizer;

        public MissilePad(Game game, SpriteBatch SB, ContentManager CM, Random randomizer)
            : base(game)
        {
            m_spriteBatch = SB;
            m_contentManager = CM;
            m_randomizer = randomizer;
            game.Components.Add(this);
        }
        public override void Initialize()
        {
            m_posX = Definitions.GUN_X;
            m_lastLaunch = - Definitions.MISSILE_COOLDOWN;

            m_lastShotTime = -Definitions.GUN_COOLDOWN;

            m_target.X = Mouse.GetState().X;
            m_target.Y = Mouse.GetState().Y;

            base.Initialize();
        }
        protected override void LoadContent()
        {
            m_launchS = m_contentManager.Load<SoundEffect>("SFX/launch");
            m_launcherT = m_contentManager.Load<Texture2D>("GFX/missilepad");
            

            //add half the height of the gun sprite to the laser start point
            Definitions.GUN_TIP_Y -= (m_launcherT.Height / 2);

            m_laserS = m_contentManager.Load<SoundEffect>("SFX/laser");
            m_crosshairT = m_contentManager.Load<Texture2D>("GFX/crosshair");

            base.LoadContent();
        }
        public override void Update(GameTime gameTime) 
        {
            //get the mouse position for the crosshair
            MouseState mouseState = Mouse.GetState();
            m_target.X = mouseState.X;
            m_target.Y = mouseState.Y;

            //if left mousebutton is pressed 
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                //and the gun is off cooldown and the target is above the ground level
                if ((gameTime.TotalGameTime.TotalMilliseconds - m_lastShotTime > Definitions.GUN_COOLDOWN)
                    && (m_target.Y < Definitions.GROUND_Y))
                {
                    m_lastShotTime = gameTime.TotalGameTime.TotalMilliseconds;
                    m_laserS.Play();
                    m_laser = new Laser(Game, m_spriteBatch, m_contentManager, m_randomizer, m_posX, m_target);
                }
            }

            //get the pressed keys for the missile pad controls
            Keys[] currentPressedKeys = Keyboard.GetState().GetPressedKeys();
            foreach (Keys key in currentPressedKeys)
            {
                //check for necessary keypresses
                if (key == Definitions.KEY_LEFT)
                {
                    m_posX -= Definitions.PAD_SPEED * (float)gameTime.ElapsedGameTime.TotalSeconds;
                }
                if (key == Definitions.KEY_RIGHT)
                {
                    m_posX += Definitions.PAD_SPEED * (float)gameTime.ElapsedGameTime.TotalSeconds;
                }
                else if ((key == Definitions.KEY_FIRE) && ((gameTime.TotalGameTime.TotalMilliseconds
                                     - m_lastLaunch) > Definitions.MISSILE_COOLDOWN))
                {
                    m_launchS.Play();
                    m_lastLaunch = (float)gameTime.TotalGameTime.TotalMilliseconds;
                    m_missile = new Missile(Game, m_spriteBatch, m_contentManager, (int)m_posX);

                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {

            //calculate the rectangle for the crosshair so that it's centered on the mouse cursor
            Rectangle targetRect = new Rectangle((int)(m_target.X - (m_crosshairT.Width / 2)),
                (int)(m_target.Y - (m_crosshairT.Height / 2)),
                m_crosshairT.Width, m_crosshairT.Height);

            m_spriteBatch.Draw(m_crosshairT, targetRect, Color.White);

            //calculate the location rectangle for the missilePad
            Rectangle padRect = new Rectangle((int)(m_posX - (m_launcherT.Width / 2)),
                (int)(Definitions.GUN_Y - (m_launcherT.Height / 2)),
                m_launcherT.Width, m_launcherT.Height);

            m_spriteBatch.Draw(m_launcherT, padRect, Color.White);


            base.Draw(gameTime);
        }
        
    }
}
